﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Sanchay
{
    public partial class SalesHistory : Form
    {
        public SalesHistory()
        {
            InitializeComponent();
        }

        private void SalesHistory_Load(object sender, EventArgs e)
        {
            dtpFrom.Value = Convert.ToDateTime("01-Apr-" + (DateTime.Today.Year));
            dtpTo.Value = Convert.ToDateTime("31-Mar-" + (DateTime.Today.Year + 1));
            SearchInvoiceList();

        }

        private void BindInvoiceListGrid(DataTable dt)
        {
            dgvSalesInvoiceList.DataSource = dt;

            dgvSalesInvoiceList.Columns[0].DataPropertyName = "Date";
            dgvSalesInvoiceList.Columns[1].DataPropertyName = "Invoice No.";
            dgvSalesInvoiceList.Columns[2].DataPropertyName = "Amount";
            dgvSalesInvoiceList.Columns[3].DataPropertyName = "Customer";

         
            dgvSalesInvoiceList.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
            dgvSalesInvoiceList.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
            dgvSalesInvoiceList.Columns[0].FillWeight = 15;
            dgvSalesInvoiceList.Columns[1].FillWeight = 30;
            dgvSalesInvoiceList.Columns[2].FillWeight = 15;
            dgvSalesInvoiceList.Columns[3].FillWeight = 40;
        }

      
        private void dgvSalesInvoiceList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SpareSalesRegister oSpareSalesRegister = new SpareSalesRegister(true, dgvSalesInvoiceList.CurrentRow.Cells[1].Value.ToString());
                oSpareSalesRegister.MdiParent = DashBoard.ActiveForm;
                oSpareSalesRegister.Show();
            }
        }

        private void txtPartsNo_TextChanged(object sender, EventArgs e)
        {
            SearchInvoiceList();
        }

      
        private void txtCustomer_TextChanged(object sender, EventArgs e)
        {
           SearchInvoiceList();
            
        }

        private void SearchInvoiceList()
        {
            DataAccess da = new DataAccess();
            string query = "SELECT CONVERT(varchar, Date, 106) AS Date, [Invoice No.], Amount, Customer FROM  vw_SalesInvoiceList " +
                "WHERE [Invoice No.] LIKE '%" + txtInvoiceNo.Text + "%'" +
               " And [Customer] LIKE '%" + txtCustomer.Text + "%'" +
               "AND Date BETWEEN '" + dtpFrom.Value.ToString("MM/dd/yyyy") + "' AND '" + dtpTo.Value.ToString("MM/dd/yyyy") + "' " +
               "ORDER BY Date DESC";
               //" AND (Date >= '" + dtpFrom.Value.ToShortDateString() + " 12:00:00AM') AND (Date <= '" + dtpTo.Value.ToShortDateString() + " 11:59:59PM')";
               
            DataTable dt = da.SelectCommand(query);
            if (dt != null)
            {
                BindInvoiceListGrid(dt);
                lblRecordCount.Text = " Total Record(s) Found: " + dt.Rows.Count.ToString();
            }
        }

        private void dtpFrom_ValueChanged(object sender, EventArgs e)
        {
            SearchInvoiceList();
        }

        private void dtpTo_ValueChanged(object sender, EventArgs e)
        {
            SearchInvoiceList();
        }

        private void dgvSalesInvoiceList_DoubleClick(object sender, EventArgs e)
        {
            SpareSalesRegister oSpareSalesRegister = new SpareSalesRegister(true, dgvSalesInvoiceList.CurrentRow.Cells[1].Value.ToString());
            oSpareSalesRegister.MdiParent = DashBoard.ActiveForm;
            oSpareSalesRegister.Show();
        }

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    DataAccess da = new DataAccess();
        //    string query = "select partsno from inventorymaster group by partsno";
        //    string query1, query2 = "";
        //    string partsNo = "";
        //    DataTable dt = da.SelectCommand(query);
        //    DataTable dt1;
        //    int inventoryid;

        //    int totalInventory = dt.Rows.Count;
        //    for (int i = 0; i < totalInventory; i++)
        //    {
        //        int availableQty = 0;
        //        Application.DoEvents();
        //        partsNo = dt.Rows[i][0].ToString();
        //        query1 = "select * from inventorymaster where partsno='" + partsNo + "' order by inventoryid asc";
        //        dt1 = da.SelectCommand(query1);

        //        for (int j = 0; j < dt1.Rows.Count; j++)
        //        {
        //            inventoryid = (int)dt1.Rows[j][0];
        //            switch ((int)dt1.Rows[j][6])
        //            {
        //                case 1:// Purchase
        //                    if (j == 0)
        //                        availableQty = (int)dt1.Rows[j][2];
        //                    else
        //                        availableQty += (int)dt1.Rows[j][2];
        //                    break;

        //                case 2:// Sales
        //                    if (j == 0)
        //                        availableQty = 0 - (int)dt1.Rows[j][2];
        //                    else
        //                        availableQty = availableQty - (int)dt1.Rows[j][2];
        //                    break;

        //                case 5:// PurchaseItemDelete
        //                    if (j == 0)
        //                        availableQty = 0 - (int)dt1.Rows[j][2];
        //                    else
        //                        availableQty = availableQty - (int)dt1.Rows[j][2];
        //                    break;

        //                case 7:// SalesItemDeletes
        //                    if (j == 0)
        //                        availableQty = (int)dt1.Rows[j][2];
        //                    else
        //                        availableQty += (int)dt1.Rows[j][2];
        //                    break;
        //            }

        //            query2 = "update inventorymaster set availableqty=" + availableQty + " where inventoryid=" + inventoryid;
        //            da.DoCommand(query2);
        //            if (j != dt1.Rows.Count - 1)
        //                da.DoCommand("update inventorymaster set iscurrent=0 where inventoryid=" + inventoryid);
        //            else
        //                da.DoCommand("update inventorymaster set iscurrent=1 where inventoryid=" + inventoryid);
        //        }

        //        progressBar1.Value = i * 100 / (totalInventory - 1);
        //        label1.Text = i.ToString();
        //    }

        //}

  
    }
}
